package com.intretech.umsin.oss.service.impl;

import com.intretech.umsin.oss.config.LocalOSSConfig;
import com.intretech.umsin.oss.entity.FileType;
import com.intretech.umsin.oss.service.MetadataStoreService;
import com.intretech.umsin.oss.util.FileUtils;
import com.intretech.umsin.oss.vo.FileInfoVo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * @version V1.0
 * @ClassName: LocalOssServiceImpl
 * @Description: LocalOssServiceImpl OSS本地存储服务实现
 * @author: 李启联
 * @date: 2022-04-25 15:31
 * @Copyright: Copyright (c) 2022
 */
@Service
public class LocalOssServiceImpl extends AbstractFileService {

    @Autowired(required = false)
    private LocalOSSConfig localOSSConfig;

    @Autowired(required = false)
    private MetadataStoreService metadataStoreService;


    @Override
    public FileType fileType() {
        return FileType.LOCAL;
    }

    /**
     * 下载文件
     *
     * @param fileInfo
     * @return
     */
    @Override
    public byte[] downloadFile(FileInfoVo fileInfo) throws Exception {
        String fileFullPath = "";
        String path = localOSSConfig.getPath().replace("\\","/");
        path = path.startsWith("/") ? path : "/"+path;
        path = path.endsWith("/") ?  path.substring(0,path.length() -1) : path;
        fileFullPath = path + fileInfo.getPath();

        File file = new File(fileFullPath);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        if(file.exists()) {
            FileInputStream is = new FileInputStream(file);
            try {
                IOUtils.copy(is, os);
                return os.toByteArray();
            } finally {
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(is);
            }
        }
        return null;
    }

    /**
     * 文件元数据存储服务
     *
     * @return
     */
    @Override
    protected MetadataStoreService getMetadataStoreService() {
        return metadataStoreService;
    }

    /**
     * 上传文件
     *
     * @param file
     * @param fileInfo
     */
    @Override
    protected void uploadFile(MultipartFile file, FileInfoVo fileInfo) throws Exception {
        String path = localOSSConfig.getPath().replace("\\","/");
        path = path.endsWith("/") ?  path.substring(0,path.length() -1) : path;

        if(fileInfo.getFilePathPrefix().length() >1){//设置文件的路径前缀
            String prefix = fileInfo.getFilePathPrefix().replace("\\","/");
            prefix = prefix.startsWith("/") ? prefix : "/"+prefix;
            prefix = prefix.endsWith("/") ? prefix.substring(0,prefix.length() -1) : prefix;
            fileInfo.setFilePathPrefix(prefix);
        }else{
            fileInfo.setFilePathPrefix("");
        }
        path = path +"/" + (fileInfo.getFilePathPrefix().length() < 1 ? "" : fileInfo.getFilePathPrefix()+"/")+fileInfo.getTagsName();
        FileUtils.saveFile(file, path);
    }

    /**
     * 删除文件资源
     *
     * @param fileInfo
     * @return
     */
    @Override
    protected boolean deleteFile(FileInfoVo fileInfo) {
        String path = localOSSConfig.getPath().replace("\\","/");
        path = path.startsWith("/") ? path : "/"+path;
        path = path.endsWith("/") ?  path.substring(0,path.length() -1) : path;
        path = path+fileInfo.getPath();
        return  FileUtils.deleteFileComplete(path);
    }

    /**
     * 对查询返回的文件列表设置其对应文件访问的URL
     *
     * @param fileInfo
     */
    @Override
    protected void setFileUrl(FileInfoVo fileInfo) {
        fileInfo.setUrl(localOSSConfig.getDomain() + localOSSConfig.getUrlPrefix() + fileInfo.getPath());
    }
}
